<?php 

	define('ACC',true);
	
	class Content extends Ci_Controller {

		public static $uName;

		public function __construct() {
			parent::__construct();
			$this->load->database();
			$this->load->helper('url');
			$this->load->library('pagination');
			$this->load->helper(array('form', 'url'));
			self::$uName = $this->session->userdata('username');
			self::$uName = addslashes(self::$uName);
 		}

/*
*	Edit By: 阿诺
*	Time : 2015.7.12
*/
		public function get_mail_byStatus(){

			$this->load->model('articleList_model');
			$data['status'] = $this->uri->segment(3);
		/*-----------------------page------------------------*/
			$page_size = 10;
 			if ( $data['status'] != 0 ) {
 				$this->db->where('status', $data['status']);
 			} 
			$this->db->from('mail')->where("from_who", self::$uName);
			$total = $this->db->count_all_results();

			$config['base_url'] = site_url('content/get_mail_byStatus/' . $data['status']);
			$config['total_rows'] = $total;
			$config['per_page'] = $page_size;
			$config['first_link'] = '首页';
			$config['last_link'] = '尾页';
			$config['prev_link'] = '上一页';
			$config['next_link'] = '下一页';
			$config['uri_segment'] = 4;

			$this->pagination->initialize($config);

			$offset = intval($this->uri->segment(4));

			$data['page'] = $this->pagination->create_links();
		/*------------------------page------------------------*/
			$data['total'] = $total;
			$data['hot'] = $this->articleList_model->count_hot(self::$uName, $data['status']);
			
			$data['art_list'] = $this->articleList_model->get_article($offset, $page_size, $data['status'], self::$uName);
			$this->load->view('admin/article/mail_list', $data);
		}

		public function del_mail(){

			$this->load->model('articleList_model');
			$id = $this->input->post("checked");

			foreach ( $id as $k_id ) {
				$this->articleList_model->move($k_id, 3);
			}

			redirect('content/get_mail_byStatus/0');
		}

		public function read_mail(){

			$this->load->model('articleList_model');
			$id = $this->input->post("checked");

			foreach ( $id as $k_id ) {
				$this->articleList_model->move($k_id, 2);
			}

			redirect('content/get_mail_byStatus/0');
		}

		public function unread_mail(){

			$this->load->model('articleList_model');
			$id = $this->input->post("checked");

			foreach ( $id as $k_id ) {
				$this->articleList_model->move($k_id, 1);
			}

			redirect('content/get_mail_byStatus/0');
		}

		public function del_mail_forever(){
			$this->load->model('articleList_model');
			$id = $this->input->post("checked");

			foreach ( $id as $k_id ) {
				$this->articleList_model->del_forever($k_id);
			}

			redirect('content/get_mail_byStatus/0');
		}

		public function move_to(){
			$this->load->model('articleList_model');

			$id = $this->input->post("checked");
			$change = $this->input->post("change");

			if ($change != 0) {
				foreach ( $id as $k_id ) {
					$this->articleList_model->move($k_id, $change);
				}
			}

			redirect('content/get_mail_byStatus/0');
		}


/*
*	Edit By: 阿诺
*	Time : 2015.7.12
*/
		public function send_mail_list(){

			$this->load->model('articleList_model');
			
		/*------------------------page------------------------*/
			$page_size = 10;
 		
			$this->db->from('send_mail')->where('send_man', self::$uName);
			$total = $this->db->count_all_results();

			$config['base_url'] = site_url('content/send_mail_list');
			$config['total_rows'] = $total;
			$config['per_page'] = $page_size;
			$config['first_link'] = '首页';
			$config['last_link'] = '尾页';
			$config['prev_link'] = '上一页';
			$config['next_link'] = '下一页';
			$config['uri_segment'] = 3;

			$this->pagination->initialize($config);

			$offset = intval($this->uri->segment(3));

			$data['page'] = $this->pagination->create_links();
		/*------------------------page---------------------------*/
			$data['total'] = $total;
			$data['hot'] = $this->articleList_model->count_hot(self::$uName);

			$data['art_list'] = $this->articleList_model->get_send_mail($offset, $page_size, self::$uName);
			$this->load->view('admin/article/send_mail_list', $data);
		}

		public function write_mail(){

			$this->load->model('articleList_model');
			
			$data['allUser'] = $this->articleList_model->get_allUser();
			
			$this->load->view('admin/article/write_mail', $data);
		}

		public function send_mail(){

			$this->load->model('articleList_model');

			$title = $this->input->post('title');
			$to_who = $this->input->post('to_who');
			$content = $this->input->post('myContent');
			$content = str_replace(", 'font-family:微软雅黑", "", $content);

			$title = addslashes($title);
			$to_who = addslashes($to_who);
			$content = addslashes($content);
			
			$this->articleList_model->send_indb($title, $to_who, $content, self::$uName);

			redirect('content/send_mail_list');

		}

		public function del_send_mail(){

			$this->load->model('articleList_model');
			$id = $this->input->post("checked");

			foreach ( $id as $k_id ) {
				$this->articleList_model->del_send_mail($k_id);
			}

			redirect('content/send_mail_list');
		}

/*
*	Edit By:
*	Time :
*/
		public function mail_content(){

			$this->load->model('articleList_model');
			$id = $this->uri->segment(3);
			$data['mail_content'] = $this->articleList_model->get_mail_content($id);
			$this->articleList_model->move($id, 2);

			$this->load->view('admin/article/mail_detail', $data);
		}

		public function send_mail_content(){

			$this->load->model('articleList_model');
			$id = $this->uri->segment(3);
			$data['mail_content'] = $this->articleList_model->get_send_mail_content($id);

			$this->load->view('admin/article/send_mail_detail', $data);
		}

		public function search(){

			$this->load->model('articleList_model');

			$key = $this->input->post("keyword");
			$key = str_replace("'", "\'", $key);

			/*page*/
			$page_size = 10;
 		
			$total = $this->articleList_model->get_search_total($key);

			$config['base_url'] = site_url('content/search');
			$config['total_rows'] = $total;
			$config['per_page'] = $page_size;
			$config['first_link'] = '首页';
			$config['last_link'] = '尾页';
			$config['prev_link'] = '上一页';
			$config['next_link'] = '下一页';
			$config['uri_segment'] = 3;

			$this->pagination->initialize($config);

			$offset = intval($this->uri->segment(3));

			$data['page'] = $this->pagination->create_links();
		/*page*/
			$data['total'] = $total;
			$data['hot'] = $this->articleList_model->count_all_hot(self::$uName);
			$data['search'] = $this->articleList_model->get_search($key,$offset,$page_size);

			$this->load->view('admin/article/search_mail_list', $data);
		}

/*------------------------------------out-------------------------------------------*/

	}


?>